Continuación Gráficos interactivos con Plotly

Michel Aza Montes

Etiquetar líneas con anotaciones

Code
library(plotly)

# Data
x <- c(2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013)
y_television <- c(74, 82, 80, 74, 73, 72, 74, 70, 70, 66, 66, 69)
y_internet <- c(13, 14, 20, 24, 20, 24, 24, 40, 35, 41, 43, 50)
data <- data.frame(x, y_television, y_internet)


xaxis <- list(
  title = "",
  showline = TRUE,
  showgrid = FALSE,
  showticklabels = TRUE,
  linecolor = 'rgb(204, 204, 204)',
  linewidth = 2,
  autotick = FALSE,
  ticks = 'outside',
  tickcolor = 'rgb(204, 204, 204)',
  tickwidth = 2,
  ticklen = 5,
  tickfont = list(family = 'Arial', size = 12, color = 'rgb(82, 82, 82)')
)

yaxis <- list(
  title = "",
  showgrid = FALSE,
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE
)


margin <- list(
  autoexpand = FALSE,
  l = 100,
  r = 100,
  t = 110
)

# Build the annotations

television_1 <- list(
  xref = 'paper',
  yref = 'y',
  x = 0.05,
  y = y_television[1],
  xanchor = 'right',
  yanchor = 'middle',
  text = ~paste('Television ', y_television[1], '%'),
  font = list(family = 'Arial', size = 16, color = 'rgba(205, 50, 120, 1)'),  # #CD3278 (rosa)
  showarrow = FALSE
)

internet_1 <- list(
  xref = 'paper',
  yref = 'y',
  x = 0.05,
  y = y_internet[1],
  xanchor = 'right',
  yanchor = 'middle',
  text = ~paste('Internet ', y_internet[1], '%'),
  font = list(family = 'Arial', size = 16, color = 'rgba(0, 134, 139, 1)'),  # #00868B (azul verdoso oscuro)
  showarrow = FALSE
)

television_2 <- list(
  xref = 'paper',
  x = 0.95,
  y = y_television[12],
  xanchor = 'left',
  yanchor = 'middle',
  text = paste('Television ', y_television[12], '%'),
  font = list(family = 'Arial', size = 16, color = 'rgba(205, 50, 120, 1)'),  # #CD3278 (rosa)
  showarrow = FALSE
)

internet_2 <- list(
  xref = 'paper',
  x = 0.95,
  y = y_internet[12],
  xanchor = 'left',
  yanchor = 'middle',
  text = paste('Internet ', y_internet[12], '%'),
  font = list(family = 'Arial', size = 16, color = 'rgba(0, 134, 139, 1)'),  # #00868B (azul verdoso oscuro)
  showarrow = FALSE
)


fig <- plot_ly(data, x = ~x) 

# Add traces with the requested colors
fig <- fig %>% add_trace(
  y = ~y_television,
  type = 'scatter',
  mode = 'lines',
  line = list(color = 'rgba(205, 50, 120, 1)', width = 2)  # #CD3278 (rosa)
)

fig <- fig %>% add_trace(
  y = ~y_internet,
  type = 'scatter',
  mode = 'lines',
  line = list(color = 'rgba(0, 134, 139, 1)', width = 4)  # #00868B (azul verdoso oscuro)
)


fig <- fig %>% add_trace(
  x = ~c(x[1], x[12]),
  y = ~c(y_television[1], y_television[12]),
  type = 'scatter',
  mode = 'markers',
  marker = list(color = 'rgba(205, 50, 120, 1)', size = 8)  # #CD3278 (rosa)
)

fig <- fig %>% add_trace(
  x = ~c(x[1], x[12]),
  y = ~c(y_internet[1], y_internet[12]),
  type = 'scatter',
  mode = 'markers',
  marker = list(color = 'rgba(0, 134, 139, 1)', size = 12)  # #00868B (azul verdoso oscuro)
)


fig <- fig %>% layout(
  title = "Main Source for News",
  xaxis = xaxis,
  yaxis = yaxis,
  margin = margin,
  autosize = FALSE,
  showlegend = FALSE,
  annotations = list(television_1, internet_1, television_2, internet_2)
)

fig

Relleno entre líneas

Code
library(plotly)


month <- c('January', 'February', 'March', 'April', 'May', 'June', 'July',
           'August', 'September', 'October', 'November', 'December')
high_2014 <- c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)
data <- data.frame(month, high_2014, low_2014)
data$average_2014 <- rowMeans(data[,c("high_2014", "low_2014")])


data$month <- factor(data$month, levels = data[["month"]])


fig <- plot_ly(data, x = ~month, y = ~high_2014, type = 'scatter', mode = 'lines',
               line = list(color = 'transparent'),
               showlegend = FALSE, name = 'High 2014') 
fig <- fig %>% add_trace(y = ~low_2014, type = 'scatter', mode = 'lines',
                         fill = 'tonexty', fillcolor='rgba(255, 131, 250, 0.2)', line = list(color = 'transparent'),
                         showlegend = FALSE, name = 'Low 2014') 
fig <- fig %>% add_trace(x = ~month, y = ~average_2014, type = 'scatter', mode = 'lines',
                         line = list(color='rgba(139, 71, 137, 1)'), # #8B4789 (Morado oscuro)
                         name = 'Average') 
fig <- fig %>% layout(title = "Average, High and Low Temperatures in New York",
                      paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
                      xaxis = list(title = "Months",
                                   gridcolor = 'rgb(255,255,255)',
                                   showgrid = TRUE,
                                   showline = FALSE,
                                   showticklabels = TRUE,
                                   tickcolor = 'rgb(127,127,127)',
                                   ticks = 'outside',
                                   zeroline = FALSE),
                      yaxis = list(title = "Temperature (degrees F)",
                                   gridcolor = 'rgb(255,255,255)',
                                   showgrid = TRUE,
                                   showline = FALSE,
                                   showticklabels = TRUE,
                                   tickcolor = 'rgb(127,127,127)',
                                   ticks = 'outside',
                                   zeroline = FALSE))

fig

Gráfica de densidad

Code
library(plotly)
library(ggplot2)


dens <- with(diamonds, tapply(price, INDEX = cut, density))
df <- data.frame(
  x = unlist(lapply(dens, "[[", "x")),
  y = unlist(lapply(dens, "[[", "y")),
  cut = rep(names(dens), each = length(dens[[1]]$x))
)

colors <- c("#EE1289", "#9932CC", "#00BFFF", "#8B4500", "#458B00")

fig <- plot_ly(df, x = ~x, y = ~y, color = ~cut, colors = colors) 
fig <- fig %>% add_lines()

fig

Escalas de colores cualitativas

Code
library(plotly)

fig <- plot_ly(data = iris, 
               x = ~Sepal.Length, 
               y = ~Petal.Length, 
               color = ~Species, 
               colors = c("#00BFFF", "#8B4500", "#458B00"))

fig

Nombres de paletas ColorBrewer

Code
library(plotly)

fig <- plot_ly(data = iris, 
               x = ~Sepal.Length, 
               y = ~Petal.Length, 
               color = ~Species, 
               colors = c("#EE1289", "#9932CC", "#00BFFF"))

fig

Mapeo de datos a símbolos

Code
library(plotly)

fig <- plot_ly(data = iris, 
               x = ~Sepal.Length, 
               y = ~Petal.Length, 
               type = 'scatter',
               mode = 'markers', 
               symbol = ~Species, 
               symbols = c('circle', 'x', 'o'),
               color = ~Species, 
               colors = c("#EE1289", "#9932CC", "#00BFFF"),
               marker = list(size = 10))

fig

Agregar asignación de color y tamaño

Code
d <- diamonds[sample(nrow(diamonds), 1000), ]

fig <- plot_ly(
  d, x = ~carat, y = ~price,
  color = ~carat, size = ~carat
)

fig

Etiquetas de datos al pasar el cursor

Code
d <- diamonds[sample(nrow(diamonds), 1000), ]

fig <- plot_ly(
  d, x = ~carat, y = ~price,
  color = ~carat, size = ~carat,
  colors = c("#EE1289", "#9932CC", "#00BFFF") 
)

fig

Gráficos de barras

Code
library(plotly)

x = c("giraffes", "orangutans", "monkeys")
y = c(20, 14, 23)

colores_personalizados <- c("#EE1289", "#9932CC", "#00BFFF")  
fig <- plot_ly(
  x = x,
  y = y,
  name = "SF Zoo",
  type = "bar",
  marker = list(color = colores_personalizados)
)

fig

Graficos de barras agrupadas

Code
library(plotly)
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)

fig <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo',
               marker = list(color = '#EE1289'))  
fig <- fig %>% add_trace(y = ~LA_Zoo, name = 'LA Zoo', marker = list(color = '#9932CC'))
fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group')

fig

Observación

ggplot2 to plotly

Con ggplotly() de Plotly, puedes convertir tus figuras ggplot2 en figuras interactivas desarrolladas con plotly.js.

Conversión de ggplot2 a plotly

Code
library(ggplot2)
library(plotly)
p=ggplot(data=mpg,mapping = aes(x=displ,y=hwy, color=class)) + geom_point() 
ggplotly(p)